/**
 *	Copyright (c) 1996-2010 Maxim Shklyar, kisla interactive, kisla.com 
 *	All rights reserved.
**/

package com.kisla.Xslidenav
{
	import flash.display.MovieClip;
	import flash.text.TextField;
	import flash.display.SimpleButton;

	import flash.events.Event;
	import flash.events.MouseEvent;

	import flash.utils.Timer;
    import flash.events.TimerEvent;


	import com.kisla.contr.ControlEvent;



	public class Xslidenav extends MovieClip {

		private var conf_xml:XML;
		private var data_xml:XML;

		private var s:Xslides;
		private var n:Xnav;

		private var curr_sl:int = 0;

		var auto_rot_tim:Timer;

// ----	--------------------------------------------	--------------------------------------------	
// ----	--------------------------------------------	--------------------------------------------

		public function Xslidenav()	{}

// ----	--------------------------------------------	--------------------------------------------	
// ----	--------------------------------------------	--------------------------------------------

		public function init(cx:XML, dx:XML) {

			conf_xml = cx;
			data_xml = dx;

			s = new Xslides(conf_xml, data_xml);
			slides_cont.addChild(s);
			s.init();

			slide_nav.init(conf_xml, data_xml);
			//n = new Xnav(conf_xml, data_xml);
			//slide_nav.nav_item_cont.addChild(n);
			//n.init();


			root.addEventListener(ControlEvent.NAV_CLICKED, set_from_nav);
			root.addEventListener(ControlEvent.NAV_NEXT, show_slide_next);
			root.addEventListener(ControlEvent.NAV_PREV, show_slide_prev);

			update();

			if (conf_xml.speed.@auto>0) {
				auto_rot_tim = new Timer(1000*conf_xml.speed.@auto);
				auto_rot_tim.addEventListener("timer", auto_next);
				auto_rot_tim.start();
			}

		}

// ----	--------------------------------------------	--------------------------------------------	
// ----	--------------------------------------------	--------------------------------------------	

		private function update(stop_auto=false):void
		{

			s.upd_slds_pos(curr_sl);
			slide_nav.upd_nav(curr_sl);

			if (stop_auto)	auto_rot_tim.stop();

		}

// ----	--------------------------------------------	--------------------------------------------	
// ----	--------------------------------------------	--------------------------------------------	

		private function set_from_nav(e:ControlEvent):void
		{
            //trace("set_from_nav: " + e.data);
			show_slide(e.data);
		}

// ----	--------------------------------------------	--------------------------------------------	
// ----	--------------------------------------------	--------------------------------------------	

		private function show_slide(i:int=0):void
		{
			if (i<0 || i>data_xml.slides.s.length()-1)		return;
			curr_sl = i;
			update(true);
		}

// ----	--------------------------------------------	--------------------------------------------	
// ----	--------------------------------------------	--------------------------------------------	

		private function auto_next(e:Event=null):void
		{
			if (curr_sl>=data_xml.slides.s.length()-1)		curr_sl=-1;
			++curr_sl;
			update();
		}


// ----	--------------------------------------------	--------------------------------------------	
// ----	--------------------------------------------	--------------------------------------------	

		private function show_slide_next(e:Event=null):void
		{
			if (curr_sl>=data_xml.slides.s.length()-1)		return;
			++curr_sl;
			update(true);
		}

// ----	--------------------------------------------	--------------------------------------------	
// ----	--------------------------------------------	--------------------------------------------	

		private function show_slide_prev(e:Event=null):void
		{
			if (curr_sl<=0)		return;
			--curr_sl;
			update(true);
		}

// ----	--------------------------------------------	--------------------------------------------	
// ----	--------------------------------------------	--------------------------------------------	

	}
}
